package demo02.actor;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import demo02.message.AgentPage;
import demo02.util.Constants;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * Created by 抽离 on 2017/6/21 0021.
 */
public class MasterActor extends AbstractActor {

    private ActorRef agentRef;



    public MasterActor(ActorRef agentRef) {
        this.agentRef = agentRef;
    }

    @Override
    public void preStart() throws Exception {
        super.preStart();
        AgentPage agentPage = new AgentPage();
        agentPage.setUrl("http://xm.anjuke.com/tycoon/");
        agentPage.setCount(Constants.RETRY_COUNT);
        agentPage.setCityId(15);
        agentRef.tell(agentPage, getSelf());
    }

    @Override
    public Receive createReceive() {
        return
                receiveBuilder()
                        .match(AgentPage.class, m -> onReceiveAgentPage(m))
                        .match(Boolean.class, m -> onReceiveAgentComplete(m))
                        .build();
    }

    private void onReceiveAgentComplete(Boolean m) {
        if(m){
            System.out.println("被通知经纪人已经爬取完成，可停止" + LocalDateTime.now());
            getContext().system().terminate();
        }
    }

    private void onReceiveAgentPage(AgentPage m) {
        agentRef.tell(m, getSelf());
    }
}
